package com.earl.gsm.web.equipment;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.earl.gsm.commons.vo.DwzMessage;
import com.earl.gsm.commons.vo.ResultMessage;
import com.earl.gsm.domain.gsm.equipment.RepositoryInfo;
import com.earl.gsm.domain.gsm.match.MatchDetail;
import com.earl.gsm.domain.sercurity.user.User;
import com.earl.gsm.service.RepositoryInfoService;
import com.earl.gsm.web.BaseController;
import com.wordnik.swagger.annotations.ApiOperation;

@Controller
@RequestMapping(value = "/equipment/repositoryInfo")
public class RepositoryInfoController extends BaseController {

	private final Logger log = Logger.getLogger(RepositoryInfoController.class);

	@Autowired
	private RepositoryInfoService service;

	@RequestMapping(value = "/repoAllMsg",produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(value = "器材库所有信息", notes = "all message of repository", httpMethod = "GET", response = RepositoryInfo.class, responseContainer = "List")
	public ModelAndView getAllMsg() {
		log.debug("get all message of repository");
		return new ModelAndView("equipment/repoAllMsg", "repoList", service.findAll());
	}
	
	@RequestMapping(value="/repoUselessMsg",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(value="器材库无效条目信息",notes="useless message of repository",httpMethod="POST",response=RepositoryInfo.class,responseContainer="List")
	public ModelAndView getUselessMsg(){
		log.debug("get useless message of repository");
		RepositoryInfo repo = new RepositoryInfo();
		repo.setUseful(false);
		return new ModelAndView("equipment/repoUselessMsg","repoList",service.searchQuery(repo));
	}
	
	@RequestMapping(value="/repoUsefulMsg",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(value="器材库有效条目信息",notes="useful message of repository",httpMethod="POST",response=RepositoryInfo.class,responseContainer="List")
	public ModelAndView getUsefulMsg(){
		log.debug("get useful message of repository");
		RepositoryInfo repo = new RepositoryInfo();
		repo.setUseful(true);
		return new ModelAndView("equipment/repoUsefulMsg","repoList",service.searchQuery(repo));
	}
	
	@RequestMapping(value="/repoRentMsg",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(value="可租借器材信息",notes="rent message of repository",httpMethod="POST",response=RepositoryInfo.class,responseContainer="List")
	public ModelAndView getRentMsg(){
		log.debug("get rent message of repository");
		RepositoryInfo repo = new RepositoryInfo();
		repo.setRentPermission(true);;
		return new ModelAndView("equipment/repoRentMsg","repoList",service.searchQuery(repo));
	}
	
	@RequestMapping(value="/repoNorentMsg",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(value="不可租借器材信息",notes="norent message of repository",httpMethod="POST",response=RepositoryInfo.class,responseContainer="List")
	public ModelAndView getUnrentMsg(){
		log.debug("get norent message of repository");
		RepositoryInfo repo = new RepositoryInfo();
		repo.setRentPermission(false);
		return new ModelAndView("equipment/repoNorentMsg","repoList",service.searchQuery(repo));
	}
	
	/**
     * 服务端预处理添加器材
     * @param
     * @return
     */
    @RequestMapping(value="/preAdd")
    @ApiOperation(value = "添加器材", notes = "add new equipment", httpMethod = "GET", response = User.class, responseContainer = "List")
    public String preAdd(){
        return "equipment/addEquipment";
    }  
    
    /**
     * 服务端添加器材
     * @param matchDetail
     * @return
     */
    @RequestMapping(value="/add",produces=MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "添加器材", notes = "add new equipment", httpMethod = "POST", response = DwzMessage.class)
    public ResponseEntity<DwzMessage> applyForMatch(RepositoryInfo repo){
    	service.save(repo);
        DwzMessage dwzMessage = new DwzMessage();
        dwzMessage.setStatusCode(200);
        dwzMessage.setMessage("添加成功");
        dwzMessage.setCallbackType("forward");
        dwzMessage.setForwardUrl("/equipment/repositoryInfo/repoAllMsg");
        dwzMessage.setNavTabId("器材详情");
        return new ResponseEntity<DwzMessage>(dwzMessage,HttpStatus.OK);
    }
    
    /**
     * 服务端删除器材
     * @return     
     */
    @RequestMapping(value="/delete/{id}",produces=MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除选中器材", notes = "delete selected equipment", httpMethod = "GET", response = DwzMessage.class, responseContainer = "List")
    public ResponseEntity<DwzMessage> allMatchApply(@PathVariable Long id){
    	service.delete(id);
        DwzMessage dwzMessage = new DwzMessage();
        dwzMessage.setCallbackType("forward");
        dwzMessage.setForwardUrl("/equipment/repositoryInfo/repoAllMsg");
        dwzMessage.setStatusCode(200);
        return new ResponseEntity<DwzMessage>(dwzMessage,HttpStatus.OK);
    }
    
    /**
     * 用户端完善器材信息
     * @return
     */
    @RequestMapping(value="/preUpdate/{id}")
    @ApiOperation(value = "预处理更新赛事信息", notes = "find All User", httpMethod = "GET", response = User.class, responseContainer = "List")
    public ModelAndView preUpdate(@PathVariable  Long id){
        return new ModelAndView("equipment/preUpdate","repo", service.findOne(id));
    }
    
    /**
     * 服务端或者用户端更新器材
     * @return
     */
    @RequestMapping(value="/update",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "更新器材信息", notes = "update equipment", httpMethod = "POST", response = DwzMessage.class)
    public ResponseEntity<DwzMessage> update(RepositoryInfo repo){
    	 service.updateByPrimaryKeySelective(repo);
    	 DwzMessage dwzMessage = new DwzMessage();
         dwzMessage.setStatusCode(200);
         dwzMessage.setMessage("更新成功");
         dwzMessage.setCallbackType("forward");
         dwzMessage.setForwardUrl("/equipment/repositoryInfo/repoAllMsg");
         dwzMessage.setNavTabId("器材详情");
        return new ResponseEntity<DwzMessage>(dwzMessage,HttpStatus.OK);
    }
}
